package cn.iocoder.an.module.mold.controller.admin.repairfaultcategory;

import cn.iocoder.an.module.mold.dal.dataobject.account.AccountDO;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.mold.controller.admin.repairfaultcategory.vo.*;
import cn.iocoder.an.module.mold.dal.dataobject.repairfaultcategory.RepairFaultCategoryDO;
import cn.iocoder.an.module.mold.service.repairfaultcategory.RepairFaultCategoryService;

@Tag(name = "管理后台 - 模具维修故障分类")
@RestController
@RequestMapping("/mold/repair-fault-category")
@Validated
public class RepairFaultCategoryController {

    @Resource
    private RepairFaultCategoryService repairFaultCategoryService;

    @PostMapping("/create")
    @Operation(summary = "创建模具维修故障分类")
    @PreAuthorize("@ss.hasPermission('mold:repair-fault-category:create')")
    public CommonResult<Long> createRepairFaultCategory(@Valid @RequestBody RepairFaultCategorySaveReqVO createReqVO) {
        return success(repairFaultCategoryService.createRepairFaultCategory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新模具维修故障分类")
    @PreAuthorize("@ss.hasPermission('mold:repair-fault-category:update')")
    public CommonResult<Boolean> updateRepairFaultCategory(@Valid @RequestBody RepairFaultCategorySaveReqVO updateReqVO) {
        repairFaultCategoryService.updateRepairFaultCategory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除模具维修故障分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mold:repair-fault-category:delete')")
    public CommonResult<Boolean> deleteRepairFaultCategory(@RequestParam("id") Long id) {
        repairFaultCategoryService.deleteRepairFaultCategory(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除模具维修故障分类")
                @PreAuthorize("@ss.hasPermission('mold:repair-fault-category:delete')")
    public CommonResult<Boolean> deleteRepairFaultCategoryList(@RequestParam("ids") List<Long> ids) {
        repairFaultCategoryService.deleteRepairFaultCategoryListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得模具维修故障分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mold:repair-fault-category:query')")
    public CommonResult<RepairFaultCategoryRespVO> getRepairFaultCategory(@RequestParam("id") Long id) {
        RepairFaultCategoryDO repairFaultCategory = repairFaultCategoryService.getRepairFaultCategory(id);
        return success(BeanUtils.toBean(repairFaultCategory, RepairFaultCategoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得模具维修故障分类分页")
    @PreAuthorize("@ss.hasPermission('mold:repair-fault-category:query')")
    public CommonResult<PageResult<RepairFaultCategoryRespVO>> getRepairFaultCategoryPage(@Valid RepairFaultCategoryPageReqVO pageReqVO) {
        PageResult<RepairFaultCategoryDO> pageResult = repairFaultCategoryService.getRepairFaultCategoryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RepairFaultCategoryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出模具维修故障分类 Excel")
    @PreAuthorize("@ss.hasPermission('mold:repair-fault-category:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRepairFaultCategoryExcel(@Valid RepairFaultCategoryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RepairFaultCategoryDO> list = repairFaultCategoryService.getRepairFaultCategoryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "模具维修故障分类.xls", "数据", RepairFaultCategoryRespVO.class,
                        BeanUtils.toBean(list, RepairFaultCategoryRespVO.class));
    }

    @GetMapping("/all")
    @Operation(summary = "获得所有模具列表")
    public CommonResult<List<RepairFaultCategoryDO>> getFaultCatAll() {
        return success(repairFaultCategoryService.getFaultCatAll());
    }
}